
class Main extends egret.DisplayObjectContainer {
    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event: egret.Event) {
        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin
            context.onUpdate = () => {
            }
        })
        egret.lifecycle.onPause = () => {
            egret.ticker.pause();
        }
        egret.lifecycle.onResume = () => {
            egret.ticker.resume();
        }
        this.runGame().catch(e => {
            console.log(e);
        })
    }

    private onResize(): void {
        //App.ControllerManager.applyFunc(ControllerConst.RpgGame, RpgGameConst.GameResize);
    }
    private async runGame() {
        let main = this

        App.StageUtils.getStage().scaleMode = egret.StageScaleMode.SHOW_ALL;
        App.StageUtils.getStage().orientation = egret.OrientationMode.LANDSCAPE;

        this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
        egret.registerImplementation("eui.IAssetAdapter", new AssetAdapter());
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());

        App.StageUtils.startFullscreenAdaptation(1136, 640, this.onResize);

        App.ResourceUtils.addConfig("resource/default.res.json", "resource/");
        //App.ResourceUtils.addConfig("resource/resource_core.json", "resource/");
        App.ResourceUtils.loadConfig(this.onConfigComplete, this);
    }

    private onConfigComplete(): void {
        App.ResourceUtils.loadGroup("preload", this.onResourceLoadComplete, this.onResourceLoadProgress, this)
    }
    private onResourceLoadProgress(itemsLoaded: number, itemsTotal: number): void {
        //App.ControllerManager.applyFunc(ControllerConsts.Loading, LoadingConst.SetProgress, itemsLoaded, itemsTotal);
    }

    private onResourceLoadComplete(param1: any, param2: any): void {
        var theme = new eui.Theme("resource/default.thm.json", this.stage);
        theme.addEventListener(eui.UIEvent.COMPLETE, this.onThemeLoadComplete, this);
    }

    private onThemeLoadComplete(): void {
        this.initScene();
        this.initModule();

        let main = this
        let loadProtoOk = function (err, root) {
            Protocol.LoadAllProtobuf(err, root)
            MyApp.getInstance()
        }

        Protocol.Root.load([
            "resource/proto/msg_def.proto",
            "resource/proto/login.proto",
            "resource/proto/battle.proto",
            "resource/proto/battle_team.proto"], { keepCase: true }, loadProtoOk)
    }

    private initScene(): void {
        //App.SceneManager.register(SceneConsts.LOADING, new LoadingScene());
        App.SceneManager.register(SceneConsts.GAME, new GameScene())
        App.SceneManager.register(SceneConsts.LODDY, new LobbyScene())
    }

    private initModule(): void {
        App.ControllerManager.register(ControllerConsts.GameMap, new GameMapController())
        App.ControllerManager.register(ControllerConsts.LobbyMainUI, new LobbyMainUIControl())
        // App.ControllerManager.register(ControllerConsts.Game, new GameUIController());
    }
}